package com.hcr.action;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.hcr.commons.DocumentPage;
import com.hcr.model.Document;
import com.hcr.service.DocumentService;
import com.opensymphony.xwork2.ActionSupport;
@Component("downloadAction")
public class DownloadAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4370160193239554032L;
	private DocumentPage documentPage;
	private DocumentService documentService;
	private String path;
	private String filename;
	private File document;
	private String documentFileName;
	public File getDocument() {
		return document;
	}
	public String getDocumentFileName() throws UnsupportedEncodingException {
		return new String(documentFileName.getBytes(),"ISO8859-1");
	}
	public DocumentPage getDocumentPage() {
		return documentPage;
	}
	public DocumentService getDocumentService() {
		return documentService;
	}
	public String getFilename() throws UnsupportedEncodingException {
		
		return new String(filename.getBytes(),"ISO8859-1");
	}
	public String getPath() {
		return path;
	}
	public void setDocument(File document) {
		this.document = document;
	}
	public void setDocumentFileName(String documentFileName) {
		this.documentFileName = documentFileName;
	}
	@Resource(name="documentPage")
	public void setDocumentPage(DocumentPage documentPage) {
		this.documentPage = documentPage;
	}
	@Resource(name="documentService")
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public java.io.InputStream getDownloadFile()throws Exception {
		System.out.println("path******"+getPath());
		InputStream in= ServletActionContext.getServletContext().getResourceAsStream(getPath());
		System.out.println(in);
		return in;
		
		 
	}
	
	public String documentDownload() throws Exception {
		HttpServletRequest request=ServletActionContext.getRequest();
		String id=request.getParameter("id");
		Document document=documentService.findById(Integer.parseInt(id));
		filename=document.getName();
		System.out.println(filename);
		path=document.getPath();
		return "success";
	}

}
